package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.VillageInfoMapper;
import com.village.zpark.pojo.VillageInfo;
import com.village.zpark.service.VillageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class VillageInfoServiceImpl implements VillageInfoService {
    @Autowired
    private VillageInfoMapper villageInfoMapper;
    @Override
    public List<VillageInfo> findAll() {
        return villageInfoMapper.selectAll();
    }

    @Override
    public VillageInfo findById(Integer id) {
        return villageInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addVillageInfo(VillageInfo villageInfo) {
        return villageInfoMapper.insert(villageInfo);
    }

    @Override
    public int updateVillageInfo(VillageInfo villageInfo) {
        return villageInfoMapper.updateByPrimaryKey(villageInfo);
    }

    @Override
    public int deleteVillageInfoById(Integer id) {
        return villageInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<VillageInfo> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(VillageInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("villagename", "%" + param + "%") // 使用 orLike 来实现多字段模糊查询
                    .orLike("location", "%" + param + "%")
                    .orLike("number", "%" + param + "%")
                    .orLike("area", "%" + param + "%");
        }
        List<VillageInfo> villageInfos;
        if (param != null && !param.isEmpty()) {
            villageInfos = villageInfoMapper.selectByExample(example);
        } else {
            villageInfos = villageInfoMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(villageInfos);
    }
}
